﻿using Inovout.Cloud.Identity.Web.Models;
using Inovout.Memberships;
using Inovout.Memberships.Models;
using Inovout.People.Memberships.Services;
using Inovout.Services;
using System;
using Inovout.Cloud.Web.Http;
using Inovout.Runtime;
using System.Web.Http;

namespace Inovout.Cloud.Identity.Web.Controllers
{
    public class SamplesController : ApiController
    {
        public const string ProviderName = "mail";
        public const string MailAccountIndentityFormat = "MailAccountIndentityFormat";

        [Route("v1/samples")]
        public MembershipUserInfo Post(SamplesMembershipUserInfo samplesMembershipUserInfo)
        {
            if (string.IsNullOrEmpty(samplesMembershipUserInfo.Email) || !samplesMembershipUserInfo.Email.EndsWith("@samples.cn"))
            {
                throw new Exception("邮箱不正确。");
            }
            string identifier = string.Format(GetMailAccountIdentityFormat(), samplesMembershipUserInfo.Email);
            samplesMembershipUserInfo.UserIdentifier = identifier;
            samplesMembershipUserInfo.Provider = ProviderName;
            var membershipUser = ComponentRegistry.Resolve<IMembershipUserService>().FindByProviderIdentifier(samplesMembershipUserInfo.Provider, samplesMembershipUserInfo.UserIdentifier);
            if (membershipUser == null)
            {
                membershipUser = ComponentRegistry.Resolve<IPersonMembershipService>().Create(samplesMembershipUserInfo.Person, samplesMembershipUserInfo.OpenIdUserCredential, MembershipUserStatus.Activate);
            }
            if (membershipUser.Status == MembershipUserStatus.Initial)
            {
                membershipUser.Status = MembershipUserStatus.Activate;
                ComponentRegistry.Resolve<IRepositoryService<MembershipUser>>().Save((MembershipUser)membershipUser);
            }
            else if (membershipUser.Status == MembershipUserStatus.Inactivate)
            {
                throw new Exception("该账户尚未激活。");
            }
            else if (membershipUser.Status == MembershipUserStatus.Locking)
            {
                throw new Exception("该账户已锁定，请与管理员联系。");
            }

            MembershipUserInfo membershipUserInfo = new MembershipUserInfo();
            membershipUserInfo.UserIdentifier = membershipUser.Name;
            membershipUserInfo.UserIdentifierCode = membershipUser.IdentifierCode;
            membershipUserInfo.Email = membershipUser.Email;
            membershipUserInfo.Name = membershipUser.Membership.Name;
            return membershipUserInfo;
        }

        private static string GetMailAccountIdentityFormat()
        {
            IApplication application = ComponentRegistry.Resolve<IApplication>();
            string url = null;
             if (application.Settings.Contains(MailAccountIndentityFormat))
            {
                url = application.Settings[MailAccountIndentityFormat].ToString();
            }
            else
            {
                url = "http://mailaccount.3cloud.com/user/{0}";
            }
            return url;
        }

    }
}